﻿<div ng-controller="AddDepotCommodityC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">添加仓库商品</h1>
    </div>
    <div class="bg-light lter b-b wrapper-md">
        <div class="input-group">
            <!--<span class="input-group-addon">品牌</span>-->
            <!--<select id="provider" class="form-control" ng-model="providerid">-->
            <!--<option value="">查看全部</option>-->
            <!--<option ng-repeat="item in providers" value={{item.Id}}>{{item.Name}}</option>-->
            <!--</select>-->
            <span class="input-group-addon">按类型查看</span>
            <select id="typename" class="form-control" ng-model="typename" ng-change="getData()">
                <option value="">全部类型</option>
                <option value="螺纹钢">螺纹钢</option>
                <option value="盘螺">盘螺</option>
                <option value="线材">线材</option>
                <option value="钢坯">钢坯</option>
            </select>

            <span class="input-group-btn" style="padding-left: 10px;">
            <button type="button" class="btn btn-default" ng-click="GoBack()">返回</button>
            </span>
        </div>
        
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table id="example1" class="table table-striped b-t b-light">
                                    <thead>
                                        <tr>
                                            <th style="text-align: center;">名称</th>
                                            <th style="text-align: center;">钢厂</th>
                                            <th style="text-align: center;">材质</th>
                                            <th style="text-align: center;">规格</th>
                                            <th style="text-align: center;">件重 </th>
                                            <th style="text-align: center;">操作</th>

                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr ng-repeat="item in commoditydata" class="text-center">
                                            <td style="width: 20%">{{item.Name}}</td>
                                            <td style="width: 20%">{{item.Brand}}</td>
                                            <td style="width: 15%">{{item.Standard}}</td>
                                            <td style="width: 15%">{{item.Format}}</td>
                                            <td style="width: 10%">{{item.Weight | number:3}}</td>

                                            <td style="width: 20%">
                                                <button class="btn btn-sm btn-default" ng-click="add(item)" data-toggle="modal" data-target="#dlg">
                                                    <i class="fa fa-plus text"></i>
                                                    <span class="text">添加</span>
                                                </button>
                                            </td>

                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="modal fade" id="dlg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close" ng-click="cancel()"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">添加商品</h4>
                </div>
                <form name="form" class="form-validation">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="control-label">{{title}}</label>
                        </div>
                        <div class="form-group">
                            <label class="control-label">单价</label>
                            <input type="text" class="form-control" ng-model="newDataItem.Price">
                        </div>
                        <div class="form-group">
                            <label class="control-label">库存</label>
                            <input type="text" class="form-control" ng-model="newDataItem.Amount">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="append()">添加</button>
                        <button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    angular.module('app') //添加仓库商品
            .controller("AddDepotCommodityC", function ($scope, $state, $http, $stateParams) {
                $scope.commoditydata = {};
                $scope.newDataItem = {};
                $scope.typename = "";
                $scope.commoditydata.DepotId = $stateParams.id;
//                获取品牌列表
                $http({
                    url: "../api/Provider?type=0&page=1&rows=9999",
                    method:"GET"
                })
                        .then(function (res) {
                            if(res.State<=0){
                                alert(res.Msg);
                            }else {
                                console.log(res.data.Data);
                                $scope.providers = res.data.Data;
                            }

                        })
                        .then(function (error) {
                            if(error){
                                console.log("error");
                            }
                        });
//                获取商品列表
                $scope.getData = function () {
//                    if(!$('#provider').val()){
//                        $scope.providerid = "";
//                    }else{
//                        $scope.providerid = $('#provider').val();
//                    }
//                    if(!$('#typename').val()){
//                        $scope.typename = "";
//                    }else{
//                        $scope.typename = $('#typename').val();
//                    }
                    console.log($stateParams.sid);
                    $http({
//                        url: "../api/Provider/Commodity?ProviderId=" + $scope.providerid + "&name=" + $scope.typename + "&format=&page=1&rows=9999",
                        url: "../api/Supplier/Commodity?supplierId="+$stateParams.sid+"&name="+$scope.typename+"&format=&page=1&rows=99999",
                        method: "GET"
                    })
                            .then(function (res) {
                                if(res.State<=0){
                                    alert(res.Msg);
                                }else {
                                    console.log(res.data.Data);
                                    $scope.commoditydata = res.data.Data;
                                }

                            })
                            .then(function (error) {
                                if (error) {
                                    alert("数据获取失败");
                                }
                            });
                };
                $scope.getData();

                //返回上一页
                $scope.GoBack = function () {
                    $state.go('app.Provider_DepotCommodityList', { 'id': $stateParams.id, 'sid': $stateParams.sid, 'Name': $stateParams.Name, 'type': $stateParams.type });
                };
//                添加商品
                $scope.add = function (item){
                    $scope.title = item.Brand+"/"+item.Name+"/"+item.Standard+"/"+item.Format;
                    $scope.newDataItem.CommodityId = item.Id;
                };
//                添加
                $scope.append = function(){
//                    console.log($scope.newDataItem);
                    $http.post('../api/Provider/AddProviderCommodity?id=' + $stateParams.id, [$scope.newDataItem])
                            .success(function(res){
                                if(res.State<=0){
                                    alert(res.Msg);
                                }else{
                                    alert("添加成功");
                                    $state.go('app.Provider_DepotCommodityList', { 'id': $stateParams.id, 'sid':$stateParams.sid , 'Name':$stateParams.Name, 'type':$stateParams.type});
                                }
                            });
                };

                //返回上一页
                $scope.abolish = function () {
                    $state.go('app.Provider_DepotCommodityList', { 'id': $scope.commoditydata.DepotId });
                };
            })

</script>